package com.hrt.manage.sequence.dao.impl;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.sql.SQLException;

import javax.persistence.Table;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.Sequence;
import com.hrt.manage.sequence.dao.SequenceDao;


@Repository
public class SequenceDaoImpl extends BaseDaoImpl<Sequence> implements SequenceDao{

public Sequence getSequence(Class clz){
	
	String tableName=clz.getSimpleName();
	if(tableName!=null){
	final String hql="select seq from Sequence seq where seq.tableName='"+tableName+"' and seq.isdelete=0";
	return this.getHibernateTemplate().execute(new HibernateCallback<Sequence>(){

		@Override
		public Sequence doInHibernate(Session session) throws HibernateException,
				SQLException {
			Query query=session.createQuery(hql);
			Sequence seq=(Sequence) query.uniqueResult();
			return seq;
			
		}
		});
	}else{
		return null;
	}
	
		
	}


/**
 * 获取表名称
 * @param clz
 * @return
 */
private String getTableName(Class clz)
{
	Annotation	tabs=clz.getAnnotation(Table.class);
	try {
		Object obj= clz.newInstance();
		Annotation tableAnnotation=obj.getClass().getAnnotation(Table.class);
		Field[] field=tableAnnotation.getClass().getDeclaredFields();
		for(Field f:field)
		{
			f.setAccessible(true);
			System.out.println(f.getName()+" = "+f.get(obj).toString());
		}
		
//		if(tableName!=null){
//			String table=tableName.toString();
//			return table;
//		}
		return null;
	} catch (IllegalArgumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SecurityException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InstantiationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}



}
